//******************************************************************************
/**
 *
 * @file	fldeff_mb_io.c
 * @brief	モンスターボール入滅
 * @author	saito
 * @data	07.06.08
 *
 */
//******************************************************************************
#include "common.h"
#include "fieldsys.h"
#include "field_effect.h"
#include "fieldobj.h"

#include "fldeff_mb_io.h"
#include "map_tool.h"
#include "pair_poke.h"


//==============================================================================
//	define
//==============================================================================

///表示位置　グリッドからのオフセットX
#define GIRD_VEC_OFFS_X (FLDOBJ_VEC_X_GRID_OFFS_FX32)
///表示位置　グリッドからのオフセットZ
#define GIRD_VEC_OFFS_Z \
	(FLDOBJ_VEC_Z_GRID_OFFS_FX32+FLDOBJ_BLACT_Z_GROUND_OFFS_FX32+(FX32_ONE*4))

//ボールを出している時間
#define BALL_APP_TIME	(2)

//ポケモンを小さくする時間
#define POKE_SMALL_TIME	(4)
//ポケモンを格納したボールを表示している時間
#define BALL_DISP_TIME	(4)
//ボール表示時間
#define PC_BALL_DISP_TIME	(16)
//描画オフセット
#define BALL_DRAW_OFS	(FX32_ONE*6)

static u16 white_pltt[32] = {
	0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,
	0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,		//通常カラー
	0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,
	0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,		//レアカラー
};


//==============================================================================
//	typedef struct
//==============================================================================
//--------------------------------------------------------------
///	FE_MB_IO構造体
//--------------------------------------------------------------
typedef struct _TAG_FE_MB_IO
{
	FE_SYS *fes;
	NNSFndAllocator allocator;
	NNSG3dResFileHeader *res_file_mb;
	NNSG3dResFileHeader *res_file_flash;
	FLD_3DOBJ_MDL mdl_ball;
	FLD_3DOBJ_MDL mdl_flash;
	FLD_3DOBJ		obj_ball;
	FLD_3DOBJ		obj_flash;
	FLD_3DOBJ_ANM anime_flash;
}FE_MB_IO;

#define FE_MB_IO_SIZE (sizeof(FE_MB_IO))	///<FE_MB_IOサイズ

//--------------------------------------------------------------
///	MB_IO_ADD_H構造体
//--------------------------------------------------------------
typedef struct
{
///	int init_gx;								///<初期X グリッド
///	int init_gy;								///<初期Y
///	int init_gz;								///<初期Z
	VecFx32 initVec;
	FIELDSYS_WORK *fsys;						///<FIELDSYS_WORK *
	FE_SYS *fes;								///<FE_SYS_PTR
	FE_MB_IO_PTR mb_io_ptr;						///<FE_MB_IO_PTR
	FIELD_OBJ_PTR fldobj;						///<対象のFIELD_OBJ_PTR
	//金銀で追加
	s16 x_ofs;
	s16 z_ofs;
	s8 dir;
}MB_IO_ADD_H;

#define MB_IO_ADD_H_SIZE (sizeof(MB_IO_ADD_H))	///<MB_IO_ADD_Hサイズ

//--------------------------------------------------------------
///	MB_IO_WORK構造体
//--------------------------------------------------------------
typedef struct
{
	int seq_no;									///<動作番号
	int obj_code;								///<対象フィールドOBJ OBJ CODE
	int obj_id;									///<対象フィールドOBJ OBJ ID
	int zone_id;								///<対象フィールドOBJ ZONE ID
	int frame;									///<フレーム
	int height_get;								///<高さ取得
	MB_IO_ADD_H head;							///<追加時のMB_IO_ADD_H

	fx32 *TargeCoordinates;
	fx32 TargetBase;
	int MoveOfs;
	VecFx32 fldobj_vec;
}MB_IO_WORK;

#define MB_IO_WORK_SIZE (sizeof(MB_IO_WORK))	///<MB_IO_WORKサイズ

//==============================================================================
//	プロトタイプ
//==============================================================================
static void MB_IO_GraphicInit( FE_MB_IO_PTR mb_io_ptr );
static void MB_IO_GraphicDelete( FE_MB_IO_PTR mb_io_ptr );

static void GetTargetVec( const u8 inDir, VecFx32 *inVec, fx32 **outTarget );
static int GetOfsMove(const u8 inDir);

static const EOA_H_NPP DATA_EoaH_MB_Out;
static const EOA_H_NPP DATA_EoaH_MB_OutFlash;
static const EOA_H_NPP DATA_EoaH_MB_In;
static const EOA_H_NPP DATA_EoaH_MB_InPC;
//--------------------------------------------------------------
/**
 * 初期化
 * @param	fes		FE_SYS_PTR
 * @retval	FE_MB_IO_PTR	FE_MB_IO_PTR
 */
//--------------------------------------------------------------
void * FE_MB_IO_Init( FE_SYS *fes )
{
	FE_MB_IO_PTR mb_io_ptr;

	mb_io_ptr = FE_AllocClearMemory( fes, FE_MB_IO_SIZE, ALLOC_FR, 0 );
	mb_io_ptr->fes = fes;
	// アロケータ作成
	sys_InitAllocator( &mb_io_ptr->allocator, HEAPID_FIELD, 32 );

	MB_IO_GraphicInit( mb_io_ptr );
	return( mb_io_ptr );
}

//--------------------------------------------------------------
/**
 * 削除
 * @param	mb_io_ptr		FE_MB_IO_PTR
 * @retval	nothing
 */
//--------------------------------------------------------------
void FE_MB_IO_Delete( void *work )
{
	FE_MB_IO_PTR mb_io_ptr = work;
	MB_IO_GraphicDelete( mb_io_ptr );
	FE_FreeMemory( mb_io_ptr );
}

//==============================================================================
//	グラフィック
//==============================================================================
//--------------------------------------------------------------
/**
 * グラフィック初期化
 * @param	mb_io_ptr	FE_MB_IO_PTR
 * @retval	nothing
 */
//--------------------------------------------------------------
static void MB_IO_GraphicInit( FE_MB_IO_PTR mb_io_ptr )
{
	NNSG3dResMdl *res_model;
	//ボール
	mb_io_ptr->res_file_mb = FE_ArcDataLoadAlloc( mb_io_ptr->fes, NARC_fldeff_monsterball_nsbmd, ALLOC_FR );
	FLD_3DObjMdlSetup( &mb_io_ptr->mdl_ball, mb_io_ptr->res_file_mb );
	FLD_3DObjInit( &mb_io_ptr->obj_ball, &mb_io_ptr->mdl_ball );

	//フラッシュ
	mb_io_ptr->res_file_flash = FE_ArcDataLoadAlloc( mb_io_ptr->fes, NARC_fldeff_mb_out_nsbmd, ALLOC_FR );
	FLD_3DObjMdlSetup( &mb_io_ptr->mdl_flash, mb_io_ptr->res_file_flash );
	FLD_3DObjInit( &mb_io_ptr->obj_flash, &mb_io_ptr->mdl_flash );
	FLD_3DObjAnmLoad( &mb_io_ptr->anime_flash, &mb_io_ptr->mdl_flash,
			ARC_FLDEFF, NARC_fldeff_mb_out_nsbta, HEAPID_FIELD, &mb_io_ptr->allocator );
	FLD_3DObjAddAnm( &mb_io_ptr->obj_flash, &mb_io_ptr->anime_flash );

}

//--------------------------------------------------------------
/**
 * グラフィック削除
 * @param	mb_io_ptr	FE_MB_IO_PTR
 * @retval	nothing
 */
//--------------------------------------------------------------
static void MB_IO_GraphicDelete( FE_MB_IO_PTR mb_io_ptr )
{
	//ボール
	FLD_3DObjMdlClean( &mb_io_ptr->mdl_ball );
	FE_FreeMemory( mb_io_ptr->res_file_mb );
	//フラッシュ
	FLD_3DObjMdlClean( &mb_io_ptr->mdl_flash );
	FE_FreeMemory( mb_io_ptr->res_file_flash );
	FLD_3DObjAnmDelete( &mb_io_ptr->anime_flash, &mb_io_ptr->allocator );

}

//--------------------------------------------------------------
/**
 * 追加
 * @param	fldobj		FIELD_OBJ_PTR
 * @retval	nothing
 */
//--------------------------------------------------------------
EOA_PTR FE_MB_IO_Add( FIELD_OBJ_PTR fldobj, MB_IO_TYPE inType)
{
	int pri;
	VecFx32 mtx;
	MB_IO_ADD_H head;
	FE_SYS *fes;
	EOA_PTR eoa;

	fes = FE_FieldOBJ_FE_SYS_Get( fldobj );

///	head.init_gx = FieldOBJ_NowPosGX_Get( fldobj );
///	head.init_gy = FieldOBJ_NowPosGY_Get( fldobj );
///	head.init_gz = FieldOBJ_NowPosGZ_Get( fldobj );
	FieldOBJ_VecPosGet( fldobj, &head.initVec );
	//描画用にオフセットをつけとく
	head.initVec.z += BALL_DRAW_OFS;

	head.x_ofs = 0;
	head.z_ofs = 0;
	head.dir = DIR_NOT;
	head.fes = fes;
	head.fsys = FE_FieldSysWorkGet( fes );
	head.mb_io_ptr = FE_EffectWorkGet( fes, FE_FLD_MBIO );
	head.fldobj = fldobj;

	FieldOBJ_VecPosGet( fldobj, &mtx );
	pri = FieldOBJ_TCBPriGet( fldobj, FLDOBJ_TCBPRI_OFFS_AFTER );
	if (inType == MB_OUT){
		eoa = FE_EoaAddNpp( fes, &DATA_EoaH_MB_Out, &mtx, TRUE, &head, pri );
	}else if(inType == MB_IN){
		eoa = FE_EoaAddNpp( fes, &DATA_EoaH_MB_In, &mtx, TRUE, &head, pri );
	}else if(inType == MB_FLASH){
		eoa = FE_EoaAddNpp( fes, &DATA_EoaH_MB_OutFlash, &mtx, TRUE, &head, pri );
	}else{
		eoa = FE_EoaAddNpp( fes, &DATA_EoaH_MB_InPC, &mtx, TRUE, &head, pri );
	}

	return eoa;
}

//--------------------------------------------------------------
/**
 * EOA 初期化
 * @param	eoa		EOA_PTR
 * @param	wk		eoa work *
 * @retval	int		TRUE=正常終了。FALSE=異常終了
 */
//--------------------------------------------------------------
static int EoaMB_Out_Init( EOA_PTR eoa, void *wk )
{
	int gx,gz;
	VecFx32 vec;
	MB_IO_WORK *work;
	const MB_IO_ADD_H *head;

	work = wk;
	head = EOA_AddPtrGet( eoa );

	work->seq_no = 0;
	work->head = *head;
	work->obj_code = FieldOBJ_OBJCodeGet( work->head.fldobj );
	work->obj_id = FieldOBJ_OBJIDGet( work->head.fldobj );
	work->zone_id = FieldOBJ_ZoneIDGet( work->head.fldobj );

///	vec.x = GRID_SIZE_FX32( work->head.init_gx );
///	vec.z = GRID_SIZE_FX32( work->head.init_gz );
///	vec.y = FieldOBJ_VecPosYGet( work->head.fldobj );
	vec = work->head.initVec;
	work->height_get = FieldOBJTool_GetHeight( work->head.fsys, &vec );

///	vec.x += GIRD_VEC_OFFS_X;
///	vec.z += GIRD_VEC_OFFS_Z;
	EOA_MatrixSet( eoa, &vec );
	//座標セット
	FLD_3DObjSetTransVec( &work->head.mb_io_ptr->obj_ball, &vec );
	FLD_3DObjSetTransVec( &work->head.mb_io_ptr->obj_flash, &vec );

	FLD_3DObjSetDraw( &work->head.mb_io_ptr->obj_flash, FALSE );	//はじめは非表示

	return( TRUE );
}

//--------------------------------------------------------------
/**
 * EOA 削除
 * @param	eoa		EOA_PTR
 * @param	wk		eoa work *
 * @retval	nothing
 */
//--------------------------------------------------------------
static void EoaMB_IO_Delete( EOA_PTR eoa, void *wk )
{
	MB_IO_WORK *work;

	work = wk;
}

//--------------------------------------------------------------
/**
 * EOA 動作
 * @param	eoa		EOA_PTR
 * @param	wk		eoa work *
 * @retval	nothing
 */
//--------------------------------------------------------------
static void EoaMB_Out_Move( EOA_PTR eoa, void *wk )
{
	int frame;
	MB_IO_WORK *work;
	FIELD_OBJ_PTR fldobj;

	work = wk;
	fldobj = work->head.fldobj;

	if( FieldOBJ_CheckSameIDOBJCodeIn(
		fldobj,work->obj_code,work->obj_id,work->zone_id) == FALSE ){
		FE_EoaDelete( eoa );
		return;
	}

	if( work->height_get == FALSE ){
		VecFx32 mtx,vec;

		EOA_MatrixGet( eoa, &mtx );

///		vec.x = GRID_SIZE_FX32( work->head.init_gx );
///		vec.z = GRID_SIZE_FX32( work->head.init_gz );
///		vec.y = mtx.y;
		vec = work->head.initVec;

		work->height_get = FieldOBJTool_GetHeight( work->head.fsys, &vec );

		if( work->height_get == TRUE ){
			mtx.y = vec.y;
			EOA_MatrixSet( eoa, &mtx );
		}
	}

	switch( work->seq_no ){
	case 0:													//揺れ

		if( ++work->frame >= BALL_APP_TIME ){
			work->seq_no = 1;
		}

		break;
	case 1:
		//ボール消して、フラッシュ開始
		FLD_3DObjSetDraw( &work->head.mb_io_ptr->obj_ball, FALSE );
		FLD_3DObjSetDraw( &work->head.mb_io_ptr->obj_flash, TRUE );
		//ポケモン表示
		PairPoke_StartVanish(fldobj, FALSE);
		FieldOBJ_DrawProcCall( fldobj );		//＜＜次の一歩でPOSとかイベントが発生して、ポーズがかかる可能性があるので、描画関数をコールしとく
		//フレームセット
		FLD_3DObjAnmSet( &work->head.mb_io_ptr->anime_flash, 0 );
		work->seq_no = 2;
	case 2:
		{
			BOOL check = FALSE;
			check = FLD_3DObjAnmNoLoop( &work->head.mb_io_ptr->anime_flash, FX32_ONE );
			if (check){
				FE_EoaDelete( eoa );
				return;
			}
		}

		break;
	}
}

//--------------------------------------------------------------
/**
 * EOA 描画
 * @param	eoa		EOA_PTR
 * @param	wk		eoa work *
 * @retval	nothing
 */
//--------------------------------------------------------------
static void EoaMB_Out_Draw( EOA_PTR eoa, void *wk )
{
	VecFx32 vec;
	MB_IO_WORK *work;
	FIELD_OBJ_PTR fldobj;

	work = wk;
	fldobj = work->head.fldobj;

	if( FieldOBJ_CheckSameIDOBJCodeIn(
		fldobj,work->obj_code,work->obj_id,work->zone_id) == FALSE ){
		FE_EoaDelete( eoa );
		return;
	}

	EOA_MatrixGet( eoa, &vec );

	FLD_3DObjDraw(&work->head.mb_io_ptr->obj_ball);
	FLD_3DObjDraw(&work->head.mb_io_ptr->obj_flash);
}

//--------------------------------------------------------------
/**
 * EOA 動作
 * @param	eoa		EOA_PTR
 * @param	wk		eoa work *
 * @retval	nothing
 */
//--------------------------------------------------------------
static void EoaMB_Out_MoveFlash( EOA_PTR eoa, void *wk )
{
	int frame;
	MB_IO_WORK *work;
	FIELD_OBJ_PTR fldobj;

	work = wk;
	fldobj = work->head.fldobj;

	if( FieldOBJ_CheckSameIDOBJCodeIn(
		fldobj,work->obj_code,work->obj_id,work->zone_id) == FALSE ){
		FE_EoaDelete( eoa );
		return;
	}

	if( work->height_get == FALSE ){
		VecFx32 mtx,vec;

		EOA_MatrixGet( eoa, &mtx );

		vec = work->head.initVec;

		work->height_get = FieldOBJTool_GetHeight( work->head.fsys, &vec );

		if( work->height_get == TRUE ){
			mtx.y = vec.y;
			EOA_MatrixSet( eoa, &mtx );
		}
	}

	switch( work->seq_no ){
	case 0:													//揺れ
		//ボール消して、フラッシュ開始
		FLD_3DObjSetDraw( &work->head.mb_io_ptr->obj_ball, FALSE );
		FLD_3DObjSetDraw( &work->head.mb_io_ptr->obj_flash, TRUE );
		//フレームセット
		FLD_3DObjAnmSet( &work->head.mb_io_ptr->anime_flash, 0 );
		work->seq_no = 1;
	case 1:
		{
			BOOL check = FALSE;
			check = FLD_3DObjAnmNoLoop( &work->head.mb_io_ptr->anime_flash, FX32_ONE );
			if (check){
				FE_EoaDelete( eoa );
				return;
			}
		}

		break;
	}
}

//--------------------------------------------------------------
/**
 * EOA 描画
 * @param	eoa		EOA_PTR
 * @param	wk		eoa work *
 * @retval	nothing
 */
//--------------------------------------------------------------
static void EoaMB_Out_DrawFlash( EOA_PTR eoa, void *wk )
{
	VecFx32 vec;
	MB_IO_WORK *work;
	FIELD_OBJ_PTR fldobj;

	work = wk;
	fldobj = work->head.fldobj;

	if( FieldOBJ_CheckSameIDOBJCodeIn(
		fldobj,work->obj_code,work->obj_id,work->zone_id) == FALSE ){
		FE_EoaDelete( eoa );
		return;
	}

	EOA_MatrixGet( eoa, &vec );

	FLD_3DObjDraw(&work->head.mb_io_ptr->obj_flash);
}

//--------------------------------------------------------------
/**
 * EOA 初期化
 * @param	eoa		EOA_PTR
 * @param	wk		eoa work *
 * @retval	int		TRUE=正常終了。FALSE=異常終了
 */
//--------------------------------------------------------------
static int EoaMB_In_Init( EOA_PTR eoa, void *wk )
{
	int gx,gz;
	VecFx32 vec;
	MB_IO_WORK *work;
	const MB_IO_ADD_H *head;

	work = wk;
	head = EOA_AddPtrGet( eoa );

	work->seq_no = 0;
	work->head = *head;
	work->obj_code = FieldOBJ_OBJCodeGet( work->head.fldobj );
	work->obj_id = FieldOBJ_OBJIDGet( work->head.fldobj );
	work->zone_id = FieldOBJ_ZoneIDGet( work->head.fldobj );

///	vec.x = GRID_SIZE_FX32( work->head.init_gx );
///	vec.z = GRID_SIZE_FX32( work->head.init_gz );
///	vec.y = FieldOBJ_VecPosYGet( work->head.fldobj );
	vec = work->head.initVec;

	work->height_get = FieldOBJTool_GetHeight( work->head.fsys, &vec );

///	vec.x += GIRD_VEC_OFFS_X;
///	vec.z += GIRD_VEC_OFFS_Z;
	EOA_MatrixSet( eoa, &vec );
	//座標セット
	FLD_3DObjSetTransVec( &work->head.mb_io_ptr->obj_ball, &vec );


	FLD_3DObjSetDraw( &work->head.mb_io_ptr->obj_ball, FALSE );	//はじめは非表示

	return( TRUE );
}

//--------------------------------------------------------------
/**
 * EOA 動作
 * @param	eoa		EOA_PTR
 * @param	wk		eoa work *
 * @retval	nothing
 */
//--------------------------------------------------------------
static void EoaMB_In_Move( EOA_PTR eoa, void *wk )
{
	int frame;
	MB_IO_WORK *work;
	FIELD_OBJ_PTR fldobj;

	work = wk;
	fldobj = work->head.fldobj;

	if( FieldOBJ_CheckSameIDOBJCodeIn(
		fldobj,work->obj_code,work->obj_id,work->zone_id) == FALSE ){
		FE_EoaDelete( eoa );
		return;
	}

	if( work->height_get == FALSE ){
		VecFx32 mtx,vec;

		EOA_MatrixGet( eoa, &mtx );

///		vec.x = GRID_SIZE_FX32( work->head.init_gx );
///		vec.z = GRID_SIZE_FX32( work->head.init_gz );
///		vec.y = mtx.y;
		vec = work->head.initVec;

		work->height_get = FieldOBJTool_GetHeight( work->head.fsys, &vec );

		if( work->height_get == TRUE ){
			mtx.y = vec.y;
			EOA_MatrixSet( eoa, &mtx );
		}
	}

	switch( work->seq_no ){
	case 0:
		{
			BLACT_WORK_PTR	act;
			FIELD_OBJ_PTR fldobj_poke = PairPoke_GetFldObjPtr(work->head.fsys);
			//連れ歩きポケモンアクター取得
			act = FldOBJ_GetPairPokeAct(work->head.fsys->fldobjsys);
			//パレット変更
			AddVramTransferManager(
					NNS_GFD_DST_3D_TEX_PLTT,		//転送セット
					NNS_GfdGetPlttKeyAddr( BLACT_GetPlttKey( act ) ),
					white_pltt,
					NNS_GfdGetPlttKeySize(BLACT_GetPlttKey( act )));

			//ポケモンの向きから収縮する方向を決定
			if (PairPoke_GetInfo_BigSize(fldobj_poke)) {			//大きいポケモンの場合
				u8 dir;
				dir = FieldOBJ_DirDispGet( fldobj_poke );
				FieldOBJ_VecPosGet( fldobj_poke, &work->fldobj_vec );
				GetTargetVec( dir, &work->fldobj_vec, &work->TargeCoordinates );
				work->TargetBase = *(work->TargeCoordinates);
				work->MoveOfs = GetOfsMove(dir);
			}else{			//小さいポケモンの場合
				work->TargeCoordinates = NULL;
			}

			work->seq_no++;
		}
	case 1:
		{
			BLACT_WORK_PTR	act;
			VecFx32 scale = {FX32_ONE,FX32_ONE,FX32_ONE};

			work->frame++;
			scale.x = FX32_ONE/(work->frame);
			scale.y = FX32_ONE/(work->frame);
			//連れ歩きポケモンアクター取得
			act = FldOBJ_GetPairPokeAct(work->head.fsys->fldobjsys);
			//連れ歩きポケモンを小さくする
			BLACT_ScaleSet(act, &scale);
			//縮小にあわせて移動
			if (work->TargeCoordinates != NULL){
				FIELD_OBJ_PTR fldobj_poke = PairPoke_GetFldObjPtr(work->head.fsys);
				*(work->TargeCoordinates) = work->TargetBase + (FX32_ONE*work->MoveOfs*work->frame/POKE_SMALL_TIME);
				FieldOBJ_VecPosSet( fldobj_poke, &work->fldobj_vec );
			}

			if( work->frame >= POKE_SMALL_TIME ){
				work->seq_no++;
				//ボール表示
				FLD_3DObjSetDraw( &work->head.mb_io_ptr->obj_ball, TRUE );
				//ポケモン非表示
				FieldOBJ_StatusBitSet_Vanish( fldobj, TRUE );
				//PairPoke_StartVanish(fldobj, TRUE);
				work->frame = 0;
			}
		}
		break;
	case 2:
		work->frame++;
		if( work->frame >= BALL_DISP_TIME ){
			FE_EoaDelete( eoa );
		}
		break;
	}
}

//--------------------------------------------------------------
/**
 * EOA 描画
 * @param	eoa		EOA_PTR
 * @param	wk		eoa work *
 * @retval	nothing
 */
//--------------------------------------------------------------
static void EoaMB_In_Draw( EOA_PTR eoa, void *wk )
{
	VecFx32 vec;
	MB_IO_WORK *work;
	FIELD_OBJ_PTR fldobj;

	work = wk;
	fldobj = work->head.fldobj;

	if( FieldOBJ_CheckSameIDOBJCodeIn(
		fldobj,work->obj_code,work->obj_id,work->zone_id) == FALSE ){
		FE_EoaDelete( eoa );
		return;
	}

	EOA_MatrixGet( eoa, &vec );

	FLD_3DObjDraw(&work->head.mb_io_ptr->obj_ball);
}

//--------------------------------------------------------------
/**
 * EOA 初期化
 * @param	eoa		EOA_PTR
 * @param	wk		eoa work *
 * @retval	int		TRUE=正常終了。FALSE=異常終了
 */
//--------------------------------------------------------------
static int EoaMB_InPC_Init( EOA_PTR eoa, void *wk )
{
	int gx,gz;
	VecFx32 vec;
	MB_IO_WORK *work;
	const MB_IO_ADD_H *head;

	work = wk;
	head = EOA_AddPtrGet( eoa );

	work->seq_no = 0;
	work->head = *head;
	work->obj_code = FieldOBJ_OBJCodeGet( work->head.fldobj );
	work->obj_id = FieldOBJ_OBJIDGet( work->head.fldobj );
	work->zone_id = FieldOBJ_ZoneIDGet( work->head.fldobj );

	vec = work->head.initVec;

	EOA_MatrixSet( eoa, &vec );
	//座標セット
	FLD_3DObjSetTransVec( &work->head.mb_io_ptr->obj_ball, &vec );


	FLD_3DObjSetDraw( &work->head.mb_io_ptr->obj_ball, FALSE );	//はじめは非表示

	return( TRUE );
}

//--------------------------------------------------------------
/**
 * EOA 動作
 * @param	eoa		EOA_PTR
 * @param	wk		eoa work *
 * @retval	nothing
 */
//--------------------------------------------------------------
static void EoaMB_InPC_Move( EOA_PTR eoa, void *wk )
{
	int frame;
	MB_IO_WORK *work;
	FIELD_OBJ_PTR fldobj;

	work = wk;
	fldobj = work->head.fldobj;

	if( FieldOBJ_CheckSameIDOBJCodeIn(
		fldobj,work->obj_code,work->obj_id,work->zone_id) == FALSE ){
		FE_EoaDelete( eoa );
		return;
	}

	switch( work->seq_no ){
	case 0:
		{
			BLACT_WORK_PTR	act;
			FIELD_OBJ_PTR fldobj_poke = PairPoke_GetFldObjPtr(work->head.fsys);
			//連れ歩きポケモンアクター取得
			act = FldOBJ_GetPairPokeAct(work->head.fsys->fldobjsys);

			//ポケモンの向きから収縮する方向を決定
			if (PairPoke_GetInfo_BigSize(fldobj_poke)) {			//大きいポケモンの場合
				u8 dir;
				dir = FieldOBJ_DirDispGet( fldobj_poke );
				FieldOBJ_VecPosGet( fldobj_poke, &work->fldobj_vec );
				GetTargetVec( dir, &work->fldobj_vec, &work->TargeCoordinates );
				work->TargetBase = *(work->TargeCoordinates);
				work->MoveOfs = GetOfsMove(dir);
			}else{			//小さいポケモンの場合
				work->TargeCoordinates = NULL;
			}

			work->seq_no++;
		}
	case 1:
		{
			BLACT_WORK_PTR	act;
			VecFx32 scale = {FX32_ONE,FX32_ONE,FX32_ONE};

			work->frame++;
			scale.x = FX32_ONE/(work->frame);
			scale.y = FX32_ONE/(work->frame);
			//連れ歩きポケモンアクター取得
			act = FldOBJ_GetPairPokeAct(work->head.fsys->fldobjsys);
			//連れ歩きポケモンを小さくする
			BLACT_ScaleSet(act, &scale);
			//縮小にあわせて移動
			if (work->TargeCoordinates != NULL){
				FIELD_OBJ_PTR fldobj_poke = PairPoke_GetFldObjPtr(work->head.fsys);
				*(work->TargeCoordinates) = work->TargetBase + (FX32_ONE*work->MoveOfs*work->frame/POKE_SMALL_TIME);
				FieldOBJ_VecPosSet( fldobj_poke, &work->fldobj_vec );
			}

			if( work->frame >= POKE_SMALL_TIME ){
				work->seq_no++;
				//ボール表示
				FLD_3DObjSetDraw( &work->head.mb_io_ptr->obj_ball, TRUE );
				//ポケモン非表示
				PairPoke_StartVanish(fldobj, TRUE);
				work->frame = 0;
			}
		}
		break;
	case 2:
		work->frame++;
		if( work->frame >= PC_BALL_DISP_TIME ){
			FE_EoaDelete( eoa );
		}
		break;
	}
}

//--------------------------------------------------------------
/**
 * ポケモンボール格納方向取得
 * @param	inDir			格納方向
 * @param	inVec			フィールドＯＢＪ位置ベクトル
 * @param	outTarget		対象座標
 *
 * @retval	nothing
 */
//--------------------------------------------------------------
static void GetTargetVec( const u8 inDir, VecFx32 *inVec, fx32 **outTarget )
{
	//方向から描画オフセットをつける
	switch(inDir){
	case DIR_UP:
	case DIR_DOWN:
		*outTarget = &(inVec->z);
		break;
	case DIR_LEFT:
	case DIR_RIGHT:
		*outTarget = &(inVec->x);
		break;
	}
}

//--------------------------------------------------------------
/**
 * ポケモンボール格納移動オフセット
 * @param	inDir			格納方向
 *
 * @retval	int				移動オフセット
 */
//--------------------------------------------------------------
static int GetOfsMove(const u8 inDir)
{
	switch(inDir){
	case DIR_UP:
		return -UPDOWN_DISP_OFS;
	case DIR_DOWN:
		return UPDOWN_DISP_OFS;
	case DIR_LEFT:
		return -RIGHTLEFT_DISP_OFS;
	case DIR_RIGHT:
		return RIGHTLEFT_DISP_OFS;
	}
	GF_ASSERT(0);
	return 0;
}

//--------------------------------------------------------------
///	EOA_H
//--------------------------------------------------------------
static const EOA_H_NPP DATA_EoaH_MB_Out =
{
	MB_IO_WORK_SIZE,
	EoaMB_Out_Init,
	EoaMB_IO_Delete,
	EoaMB_Out_Move,
	EoaMB_Out_Draw,
};

static const EOA_H_NPP DATA_EoaH_MB_OutFlash =
{
	MB_IO_WORK_SIZE,
	EoaMB_Out_Init,
	EoaMB_IO_Delete,
	EoaMB_Out_MoveFlash,
	EoaMB_Out_DrawFlash,
};

static const EOA_H_NPP DATA_EoaH_MB_In =
{
	MB_IO_WORK_SIZE,
	EoaMB_In_Init,
	EoaMB_IO_Delete,
	EoaMB_In_Move,
	EoaMB_In_Draw,
};

static const EOA_H_NPP DATA_EoaH_MB_InPC =
{
	MB_IO_WORK_SIZE,
	EoaMB_InPC_Init,
	EoaMB_IO_Delete,
	EoaMB_InPC_Move,
	EoaMB_In_Draw,
};